package com.qiaoba.module.system.controller;

import com.qiaoba.api.log.annotation.Log;
import com.qiaoba.api.log.enums.BusinessType;
import com.qiaoba.api.system.entity.dto.SysMenuDTO;
import com.qiaoba.common.base.entity.AjaxResult;
import com.qiaoba.common.base.entity.Result;
import com.qiaoba.common.base.validate.AddGroup;
import com.qiaoba.common.base.validate.EditGroup;
import com.qiaoba.module.system.entity.SysMenu;
import com.qiaoba.module.system.entity.param.SysMenuParam;
import com.qiaoba.module.system.service.SysMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Size;
import java.util.List;

/**
 * 菜单管理 Web层
 *
 * @author ailanyin
 * @version 1.0
 * @since 2023/5/5 10:06
 */
@Validated
@RestController
@RequestMapping("/system/menu")
@RequiredArgsConstructor
@Tag(name = "菜单管理")
public class SysMenuController {

    private final SysMenuService sysMenuService;

    @PreAuthorize("hasAuthority('system:menu:list')")
    @GetMapping("/list")
    @Operation(summary = "获取列表")
    public Result<List<SysMenu>> list(@Validated SysMenuParam param) {
        List<SysMenu> menus = sysMenuService.selectList(param);
        return Result.success(menus);
    }

    @PreAuthorize("hasAuthority('system:menu:query')")
    @GetMapping(value = "/{menuId}")
    @Operation(summary = "获取详情")
    public Result<SysMenu> getInfo(@Size(max = 20, message = "ID不合法") @PathVariable String menuId) {
        return Result.success(sysMenuService.selectById(menuId, false));
    }

    @Log(title = "菜单管理", businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('system:menu:add')")
    @PostMapping
    @Operation(summary = "添加菜单")
    public Result add(@Validated(AddGroup.class) @RequestBody SysMenuDTO dto) {
        return Result.toAjax(sysMenuService.insert(dto));
    }

    @Log(title = "菜单管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('system:menu:edit')")
    @PutMapping
    @Operation(summary = "修改菜单")
    public Result edit(@Validated(EditGroup.class) @RequestBody SysMenuDTO dto) {
        return Result.toAjax(sysMenuService.updateById(dto));
    }

    @Log(title = "菜单管理", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('system:menu:remove')")
    @DeleteMapping("/{menuId}")
    @Operation(summary = "删除菜单")
    public Result remove(@PathVariable("menuId") String menuId) {
        return Result.toAjax(sysMenuService.deleteById(menuId));
    }

    @GetMapping("/tree-select")
    @Operation(summary = "构建菜单树")
    public Result treeSelect(SysMenuParam param) {
        List<SysMenu> menus = sysMenuService.selectList(param);
        return Result.success(sysMenuService.buildMenuTree(menus));
    }

    @GetMapping(value = "/role-menu-tree-select/{roleId}")
    @Operation(summary = "角色菜单树")
    public AjaxResult roleMenuTreeSelect(@PathVariable("roleId") String roleId) {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", sysMenuService.selectMenuIdsByRoleId(roleId));
        ajax.put("menus", sysMenuService.buildMenuTree(sysMenuService.selectList(new SysMenuParam())));
        return ajax;
    }
}
